<?php
/**
 * Created by PhpStorm.
 * User: chenjz
 * Date: 2017/4/24
 * Time: 11:27
 * 常用的一些方法
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Common {

    //处理时间显示规则的函数(y-M-d h:m:s)
    public static function deal_time_format($timeStr) {
        date_default_timezone_set('PRC');
        $result = "";
        $timeStamp = strtotime($timeStr);
        $nowTimeStamp = time();
        $days = floor(($nowTimeStamp - $timeStamp)/(60*60*24));
        //小于24小时
        if(!$days){
            $result = date("H:i",$timeStamp);
        }
        //大于24小于48小时
        else if($days <= 3){
            $result = $days.'天前';
        }
        else if($days <= 6){
            $result = '3天前';
        }
        else if($days <= 30){
            $result = '1周前';
        }
        else if($days <= 60){
            $result = '1个月前';
        }
        else if($days <= 365){
            $result = date("MM-d",$timeStamp);
        }
        else{
            $result = date("YYYY-MM",$timeStamp);
        }
        return $result;
    }

    /**
     * @param $url
     * @param $post
     * @param $data 请求参数
     * @return mixed json字符串
     */
    public static function curl($url, $post = false, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        if ($post) {
            curl_setopt($curl, CURLOPT_POST, 1 );//请求方式为post
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data );//post传输的数据
        }
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }

    /**
     * 根据月份和日期获取星座
     * @param $month
     * @param $day
     * @return mixed
     */
    public static function getConstellationByMonthAndDay($month, $day) {
        // 星座名称以及开始日期
        $constellations = array(
         array( "20" => "水瓶座"),
         array( "19" => "双鱼座"),
         array( "21" => "白羊座"),
         array( "20" => "金牛座"),
         array( "21" => "双子座"),
         array( "22" => "巨蟹座"),
         array( "23" => "狮子座"),
         array( "23" => "处女座"),
         array( "23" => "天秤座"),
         array( "24" => "天蝎座"),
         array( "22" => "射手座"),
         array( "22" => "摩羯座")
        );

        list($constellation_start, $constellation_name) = each($constellations[(int)$month-1]);

        if ($day < $constellation_start) list($constellation_start, $constellation_name) = each($constellations[($month -2 < 0) ? $month = 11: $month -= 2]);

        return $constellation_name;
    }

    /**
     * 根据年份获得生肖
     * @param $year
     * @return mixed
     */
    public function getAnimal($year) {
        $animals = array(
            '鼠', '牛', '虎', '兔', '龙', '蛇',
            '马', '羊', '猴', '鸡', '狗', '猪'
        );
        $key = ($year - 1900) % 12;
        return $animals[$key];
    }

    /**
     * 生成带参数的二维码
     * @param $data
     * @return mixed
     */
    public function getQrCode($data) {
        $accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx668f24dc341fc278&secret=5632f2b939a1fbe1ba23a1bfa4854714";
        $accessToken = json_decode(Common::curl($accessTokenUrl, false), true)['access_token'];
        $qrCodeUrl = "https://api.weixin.qq.com/wxa/getwxacode?access_token=$accessToken";
        $stream = Common::curl($qrCodeUrl, true, $data);
        return $stream;
    }

    /**
     * 获取accessToken
     * @param $data
     * @return mixed
     */
    public function sendTempMsg($data) {
        $accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx668f24dc341fc278&secret=5632f2b939a1fbe1ba23a1bfa4854714";
        $accessToken = json_decode(Common::curl($accessTokenUrl, false), true)['access_token'];
        $tempMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$accessToken";
        $res = Common::curl($tempMsgUrl, true, $data);
        return $res;
    }

}